import {ActionMapValueType} from "../action/ActionMethodsBase";
import ActionParam from "../action/ActionParam";

export type InterceptorLoadHandleType = (actionParam: ActionParam) => boolean;
export type InterceptorBeforeHandleType = (actionParam: ActionParam, routeInfo: ActionMapValueType) => boolean;
export type InterceptorAfterHandleType = (actionParam: ActionParam, routeInfo: ActionMapValueType, handleResult: any) => boolean;

class ActionInterceptor {

    // 拦截器名
    name?: string
    // 执行顺序，如果重复则按添加的顺序，没有顺序的优先级最低
    sort?: number
    // 解析路由之前的拦截器
    loadHandle?: InterceptorLoadHandleType
    // 执行路由处理方法之前的拦截器
    beforeHandle?: InterceptorBeforeHandleType
    // 执行路由处理方法之后的拦截器
    afterHandle?: InterceptorAfterHandleType

    constructor(name?: string) {
        this.name = name
    }

    /**
     * 设置执行顺序，如果重复则按添加的顺序，没有顺序的优先级最低
     * @param {number} sort 执行顺序
     * @return {ActionInterceptor}
     */
    setSort(sort: number): ActionInterceptor {
        if (typeof sort !== "number") {
            throw new Error("Sort must be a number!");
        }
        this.sort = sort
        return this
    }

    /**
     * 解析路由之前的拦截器
     * @param {InterceptorLoadHandleType} handle 处理器
     * @return {ActionInterceptor}
     */
    load(handle: InterceptorLoadHandleType): ActionInterceptor {
        this.loadHandle = handle
        return this
    }

    /**
     * 执行路由处理方法之前的拦截器
     * @param {InterceptorBeforeHandleType} handle 处理器
     * @return {ActionInterceptor}
     */
    before(handle: InterceptorBeforeHandleType): ActionInterceptor {
        this.beforeHandle = handle
        return this
    }

    /**
     * 执行路由处理方法之后的拦截器
     * @param {InterceptorAfterHandleType} handle 处理器
     * @return {ActionInterceptor}
     */
    after(handle: InterceptorAfterHandleType): ActionInterceptor {
        this.afterHandle = handle
        return this
    }
}

export default ActionInterceptor
